using System;

namespace Meta.Editor
{
    /// <summary>
    /// Summary description for CompletionWalker.
    /// </summary>
    public class CompletionWalker : Reader
    {
        CodeBox code;
        string prefix;

        CompletionWalker(CodeBox code, EditorTextReader stream, string prefix)
            : base(stream, stream.scanner.charIndex)
        {
            this.code = code;
            this.prefix = prefix;
        }

        public static void WalkCompletions(CodeBox code, int start, string prefix)
        {
            CompletionWalker g = new CompletionWalker(code, new EditorTextReader(code.ScanCharsFrom(start)), prefix);
            try
            {
                g.Read();
            }
            catch { }
        }

        public override void EndElement(System.Collections.ArrayList l)
        {
            if (l.Count == 0)
                return;
            object last = l[l.Count - 1];
            if (last == null || !(last is Meta.Symbol))
                return;
            Symbol s = (Symbol)last;
            if (s.name != prefix)
                code.TryCompletion(prefix, s.name);

        }
    }
}
